<template>
  <div class="main">
    <div class="main-content">
      <div class="left-main-content">
        <div class="name-jurisdiction">
          <div class="name">
            <span class="name-title">你好,</span>
            <span class="name-content">
              {{ userStatus.userName }}
            </span>
          </div>
          <div class="jurisdiction">
            <span class="jurisdiction-title">你的角色:</span>
            <span class="jurisdiction-content" v-if="userStatus.userRole === 3">
              管理员
            </span>
            <span class="jurisdiction-content" v-if="userStatus.userRole === 2">
              店主
            </span>
          </div>
        </div>
        <div class="orgNum">
          <div
              class="platform-data"
              v-for="(item, index) in shopNumStatus"
              :key="index"
          >
            <div class="title">{{ item.title }}</div>
            <div class="info">{{ item.num }}</div>
          </div>
        </div>
        <div class="applyNewShop">
          <div class="label">
            申请一个新商店
          </div>
          <el-button type="primary" @click="router.push('/shopOwner/applyNewShop')">申请</el-button>
        </div>
      </div>
      <div class="right-main-content">
        <div class="echarts">
          <div class="title">
            <span>本店订单统计图</span>
            <el-select
                v-model="chartStatus.orderType"
                placeholder="Select"
                size="small"
                @change="getOrderTrend"
            >
              <el-option
                  v-for="item in orderChart.options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </div>
          <div class="main-echarts" ref="orderEchartsRef"></div>
        </div>
        <div class="echarts">
          <div class="title">
            <span>本店销售额统计图</span>
            <el-select
                v-model="chartStatus.saleType"
                placeholder="Select"
                size="small"
                @change="getSaleTrend"
            >
              <el-option
                  v-for="item in orderChart.options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
            </el-select>
          </div>
          <div class="main-echarts" ref="saleEchartsRef"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {onMounted, reactive, ref} from "vue";
import {httpPostManage} from "@/api/httpService";
import * as echarts from "echarts";
import router from "@/router";

const orderEchartsRef = ref(null);

const saleEchartsRef = ref(null);

const userStatus = reactive({
  userName: "名字",
  userRole: 1
})

/** 左中数字相关*/
const shopNumStatus = reactive([
  {
    title: "商店总数",
    name: "shopNum",
    num: 0
  },
  {
    title: "待审核商店数",
    name: "shopExamineNum",
    num: 0
  },
  {
    title: "所有商店菜品总数",
    name: "mealNum",
    num: 0
  },
  {
    title: "所有商店待处理订单总数",
    name: "needHandleNum",
    num: 0
  },
  {
    title: "本月订单总数",
    name: "monthlyOrderNum",
    num: 0
  },
  {
    title: "本月订单总额",
    name: "monthlyOrderMoney",
    num: 0
  }
]);

const orderChart = reactive({
  options: [
    {
      label: "2022",
      value: "2022"
    },
    {
      label: "2023",
      value: "2023"
    }
  ]
});

const chartStatus = reactive({
  orderType: "2023",
  saleType: "2023"
});

const getShopNumInfo = async () => {
  let url = "getShopNumInfo";
  let res = await httpPostManage(url, {});
  shopNumStatus.map((item) => {
    item.num = res.data[item.name];
    return item;
  });
  for (let p in userStatus) {
    userStatus[p] = res.data[p];
  }
}

const getOrderTrend = async () => {
  let url = "/getOrderTrend";
  let params = {
    createTime: chartStatus.orderType
  }
  let res = await httpPostManage(url, params);
  initOrderChart(res.data);
}

const initOrderChart = (yData) => {
  let chartInstance = null;
  const echartsOption = {
    title: {
      text: "月订单销售量"
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow"
      }
    },
    xAxis: {
      type: "category",
      data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
    },
    yAxis: {
      type: "value",
      minInterval: 1
    },
    grid: {
      left: "5%",
      right: "5%",
      top: "15%",
      bottom: "15%"
    },
    series: [
      {
        data: yData,
        type: "bar",
        showBackground: true,
        backgroundStyle: {
          color: "rgba(180, 180, 180, 0.2)"
        },
        // barWidth: chartStatus.trendType == 1 ? 70 : null,
        itemStyle: {
          color: "#e85713"
        }
      }
    ]
  };

  let myChart = echarts.getInstanceByDom(orderEchartsRef.value);
  if (myChart == null) {
    chartInstance = echarts.init(orderEchartsRef.value);
    chartInstance.setOption(echartsOption);
  } else {
    myChart.setOption(echartsOption);
  }
};

const getSaleTrend = async () => {
  let url = "/getSaleTrend";
  let params = {
    createTime: chartStatus.saleType
  }
  let res = await httpPostManage(url, params);
  initSaleChart(res.data);
}

const initSaleChart = (yData) => {
  let chartInstance = null;
  const echartsOption = {
    title: {
      text: "月销售额"
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow"
      }
    },
    xAxis: {
      type: "category",
      data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"]
    },
    yAxis: {
      type: "value",
      minInterval: 1
    },
    grid: {
      left: "5%",
      right: "5%",
      top: "15%",
      bottom: "15%"
    },
    series: [
      {
        data: yData,
        type: "bar",
        showBackground: true,
        backgroundStyle: {
          color: "rgba(180, 180, 180, 0.2)"
        },
        // barWidth: chartStatus.trendType == 1 ? 70 : null,
        itemStyle: {
          color: "#e85713"
        }
      }
    ]
  };

  let myChart = echarts.getInstanceByDom(saleEchartsRef.value);
  if (myChart == null) {
    chartInstance = echarts.init(saleEchartsRef.value);
    chartInstance.setOption(echartsOption);
  } else {
    myChart.setOption(echartsOption);
  }
};

onMounted(async () => {
  await getShopNumInfo();
  await getOrderTrend();
  await getSaleTrend();
});
</script>

<style lang="scss" scoped>
.main {
  min-height: calc(100vh - 64px - 60px);
  height: auto;
  width: calc(100vw - 200px - 120px);
  background-color: #f9f9f9;
  padding: 10px;
  .main-content {
    min-height: 670px;
    height: auto;
    width: calc(100vw - 200px - 140px);
    background-color: white;
    border-radius: 5px;
    display: flex;
    padding: 30px;
    .left-main-content {
      width: 520px;
      height: auto;
      border-right: 1px solid #e2e2e2;
      padding-right: 30px;
      .name-jurisdiction {
        min-height: 100px;
        height: auto;
        width: 100%;
        border-bottom: 1px solid #e2e2e2;
        padding-left: 15px;
        padding-bottom: 10px;
        .name {
          height: 45%;
          width: 100%;
          .name-title {
            font-size: 23px;
          }
          .name-content {
            font-size: 23px;
            margin-left: 20px;
          }
        }
        .jurisdiction {
          margin-top: 20px;
          min-height: 30%;
          height: auto;
          width: 100%;
          .jurisdiction-title {
            color: #848684;
            font-size: 15px;
          }
          .jurisdiction-content {
            font-size: 15px;
            margin-left: 10px;
            color: #848684;
          }
        }
      }
      .orgNum {
        height: 420px;
        width: 100%;
        border-top: 1px solid #e2e2e2;
        border-bottom: 1px solid #e2e2e2;
        padding: 15px;
        display: flex;
        flex-wrap: wrap;
        .platform-data {
          width: 50%;
          height: 25%;
          .title {
            margin-top: 10px;
            width: 90%;
            color: #777777;
          }
          .info {
            margin-top: 10px;
            height: 40%;
            font-size: 25px;
            font-weight: bold;
          }
        }
      }
      .applyNewShop {
        margin-top: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        .label {
          font-size: 16px;
          margin-right: 50px;
        }
      }
    }
    .right-main-content {
      height: auto;
      flex: 1;
      width: 0;
      padding-left: 30px;
      .echarts {
        padding: 0 15px;
        padding-right: 25px !important;
        height: 350px;
        width: 100%;
        .title {
          width: 100%;
          height: 40px;
          display: flex;
          justify-content: space-between;
          align-items: center;
        }
        .main-echarts {
          border: 1px solid #e2e2e2;
          margin-top: 10px;
          height: 270px;
          width: 100%;
        }
      }
      .application {
        margin-top: 10px;
        width: 100%;
        height: auto;
        .title {
          line-height: 22px;
        }
        .function-main {
          height: auto;
          width: 100%;
          display: flex;
          justify-content: space-between;
          flex-wrap: wrap;
          .item {
            width: 30%;
            height: 80px;
            border-radius: 3px;
            border: 1px solid #d5d5d5;
            cursor: pointer;
            display: flex;
            margin-top: 20px;
            .left-item {
              height: 100%;
              width: 80%;
              display: flex;
              align-items: center;
              padding-left: 20px;
              .picture {
                width: 50px;
                height: 50px;
              }
            }
            .left-item .title-span {
              margin-left: 20px;
              color: #101010;
              font-size: 16px;
            }
            .left-item:hover .title-span {
              margin-left: 20px;
              color: #3894ff;
              font-size: 16px;
            }
          }
          .item .right-item {
            height: 100%;
            width: 20%;
            display: none;
          }
          .item:hover .right-item {
            height: 100%;
            width: 20%;
            display: inline-block;
            .right-center-item {
              margin-top: 10px;
              height: 50px;
              width: 100%;
              display: flex;
              align-items: center;
              justify-content: center;
              .setting {
                width: 24px;
                height: 24px;
                font-size: 24px;
                margin-top: 10px;
                :hover {
                  color: #3894ff;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>